package com.yun.zf.admin.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.yun.zf.common.admin.domain.SysDictType;
import com.yun.zf.common.admin.dto.sys.SysDictTypesDto;
import com.yun.zf.common.admin.model.sys.SysDictTypeAddMo;
import com.yun.zf.common.admin.model.sys.SysDictTypeSearchMo;
import com.yun.zf.common.annotation.LogPrint;
import com.yun.zf.common.constant.UserConstants;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.exception.CustomException;
import com.yun.zf.system.service.ISysDictDataService;
import com.yun.zf.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dict/type")
@Api(tags = "字典类型管理")
@Slf4j
public class SysDictTypeController {
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private ISysDictDataService dataService;

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @PostMapping("/listType")
    @ApiOperation(value = "字典类型列表")
    @LogPrint(description = "字典类型列表")
    public Response listType(@RequestBody SysDictTypeSearchMo dictType) {
        return Response.success(dictTypeService.selectDictTypeList(dictType));
    }


    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/type/{dictId}")
    @ApiOperation(value = "根据id查询字典类型详细信息")
    @LogPrint(description = "根据id查询字典类型详细信息")
    public Response<SysDictType> getInfoType(@PathVariable Long dictId) {
        return Response.success(dictTypeService.selectDictTypeById(dictId));
    }


    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @PostMapping("/addType")
    @ApiOperation(value = "新增字典类型")
    @LogPrint(description = "新增字典类型")
    public Response addType(@Validated @RequestBody SysDictTypeAddMo dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(null, dict.getDictType()))) {
            return Response.error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.insertDictType(dict);
        return Response.success();
    }


    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @PostMapping("/editType")
    @ApiOperation(value = "修改字典类型内容")
    @LogPrint(description = "修改字典类型内容")
    public Response editType(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict.getDictId(), dict.getDictType()))) {
            return Response.error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.updateDictType(dict);
        return Response.success();
    }


    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @ApiOperation(value = "删除字典类型")
    @PostMapping("/removeType")
    @LogPrint(description = "删除字典类型")
    @ApiOperationSupport(
            params = @DynamicParameters(properties = {
                    @DynamicParameter(value = "类型id数组", name = "dictType")
            })
    )
    public Response removeType(@RequestBody Map<String, String[]> map) {
        try {
        //查询是否有子项
        for (String type : map.get("dictType")) {
            int count = dataService.hasDataByType(type);
            if (count >= 1) {
                return Response.error(type + "存在字典数据不能删除");
            }
        }

            dictTypeService.deleteDictByTypes(map.get("dictType"));
        } catch (Exception e) {
            return Response.error();
        }

        return Response.success();
    }

    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @PostMapping(value = "/getTypeAndName")
    @ApiOperation(value = "查询所有字典类型集合")
    public Response<List<SysDictTypesDto>> getTypeAndName() {
        return Response.success(dictTypeService.getTypes());
    }


}
